﻿Imports System.Linq
Imports System.Data
Imports System.Web
Imports Equipment.DataAccess

Partial Public Class UserNotification

#Region "Properties"
    Private _db As EquipmentDataContext
    Public Property DBContext As EquipmentDataContext
        Get
            Return _db
        End Get
        Set(ByVal value As EquipmentDataContext)
            _db = value
        End Set
    End Property


#End Region

    Public Shared Function RetrieveAll() As IEnumerable(Of UserNotification)
        Dim db As EquipmentDataContext = Database.Instance
        Return db.UserNotifications.OrderBy(Function(unt) unt.Description)
    End Function


    Public Shared Function RetrieveAllByTrigger(ByVal trigger As NotificationTrigger) As IEnumerable(Of UserNotification)
        Dim db As EquipmentDataContext = Database.Instance
        Return From n In Database.Instance.UserNotifications Where n.NotificationTrigger = trigger Order By n.Description
    End Function

    Public Shared Function RetrieveAllFor(ByVal userID As Integer) As IEnumerable(Of UserNotification)
        Dim db As EquipmentDataContext = Database.Instance
        Return From n In Database.Instance.UserNotifications Where n.UserID = userID Order By n.Description
    End Function

    Public Shared Function GetByPrimaryKey(ByVal id As Integer) As UserNotification
        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From nt In db.UserNotifications Where nt.ID = id
        Dim notificationEntity As UserNotification = result.FirstOrDefault()
        notificationEntity.DBContext = db
        Return notificationEntity
    End Function


    Public Shared Sub Insert(ByVal entity As UserNotification)
        Dim db As EquipmentDataContext = Database.Instance

        Dim newNotification As New UserNotification

        newNotification.OverdueType = entity.OverdueType
        newNotification.AssetTypeID = entity.AssetTypeID
        newNotification.DepartmentID = entity.DepartmentID
        newNotification.Description = entity.Description
        newNotification.NotificationTrigger = entity.NotificationTrigger
        newNotification.OverdueXValue = entity.OverdueXValue
        newNotification.UserID = entity.UserID

        db.UserNotifications.InsertOnSubmit(newNotification)
        Try
            db.SubmitChanges()
        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try
    End Sub

    Public Sub Save()

        Dim currNotification = UserNotification.GetByPrimaryKey(Me.ID)

        Dim db As EquipmentDataContext = currNotification.DBContext

        currNotification.OverdueType = Me.OverdueType
        currNotification.AssetTypeID = Me.AssetTypeID
        currNotification.DepartmentID = Me.DepartmentID
        currNotification.Description = Me.Description
        currNotification.NotificationTrigger = Me.NotificationTrigger
        currNotification.OverdueXValue = Me.OverdueXValue
        currNotification.UserID = Me.UserID

        Try
            db.SubmitChanges()
        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try

    End Sub

    Public Sub Delete()
        If Me.DBContext IsNot Nothing Then
            Me.DBContext.UserNotifications.DeleteOnSubmit(Me)

            Try
                Me.DBContext.SubmitChanges()
            Catch ex As Exception
                ' logger.Error(exception.Message + ":" + exception.StackTrace);
            End Try
        End If
    End Sub

End Class
